package com.ht.medicaltreatment.activity.haocai2

import android.content.Intent
import android.text.TextUtils
import android.view.View
import androidx.activity.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.GsonUtils
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.listener.OnItemChildClickListener
import com.gym.baselib.base.BaseActivity
import com.ht.medicaltreatment.R
import com.ht.medicaltreatment.bean.PrepareConsumableFileBean
import com.ht.medicaltreatment.bean.PrepareData
import com.ht.medicaltreatment.utils.ScanUtils
import com.ht.medicaltreatment.view.RefrigeratorLocationDialog
import com.ht.medicaltreatment.viewmodel.BindingConsumableViewModel
import com.xgst.lib.utils.showToast
import kotlinx.android.synthetic.main.activity_create_consumable_layout.recyclerView
import kotlinx.android.synthetic.main.activity_create_consumable_layout.tvBinding
import kotlinx.android.synthetic.main.activity_create_consumable_layout.tvSampleBoxCount

//创建耗材  绑定样本盒
class CreateConsumableActivity : BaseActivity(), OnItemChildClickListener {
    override fun getLayoutId() = R.layout.activity_create_consumable_layout

    private val model by viewModels<BindingConsumableViewModel>()
    private var isScan = false
    private var sampleBoxStruId = ""
    private var checkPosition = -1
    private var sampleBoxId = -1
    private var id = -1
    private lateinit var adapter:CreateConsumableTopAdapter

    override fun init() {
        setTitle("创建耗材")
        ScanUtils.init()

        id = intent.getIntExtra("id", -1)
        val json = intent.getStringExtra("json")
        val bean = GsonUtils.fromJson(json, PrepareConsumableFileBean::class.java)

        recyclerView.layoutManager = LinearLayoutManager(this)
        adapter = CreateConsumableTopAdapter()
        recyclerView.adapter = adapter
        if (bean != null) {
            tvSampleBoxCount.text = "样本盒数量：${bean.data.size}"
            adapter.setNewInstance(bean.data)
        }
        adapter.setOnItemChildClickListener(this)

        if(id != -1){
            showProgress()
            model.requestList(id, {
                dismissProgress()
                adapter.setNewInstance(it)
                tvSampleBoxCount.text = "样本盒数量：${it.size}"
            })
        }

        //绑定rfid
        tvBinding.setOnClickListener {
            val bean = adapter.data.filter { it.isChecked }
            if (bean.size == 0) {
                showToast("还未勾选样本盒")
                return@setOnClickListener
            }

            sampleBoxId = bean[0].id
            //选择样本盒位置
            RefrigeratorLocationDialog(this).show { name, id ->
                sampleBoxStruId = id
                isScan = true
                ScanUtils.open {
                    ScanUtils.close()
                    bindingData(it)
                }
            }
        }
    }

    private fun bindingData(rfidCode: String) {
        if (isScan) {
            isScan = false
            showProgress()
            model.bindding(rfidCode,"$sampleBoxId",sampleBoxStruId){
                dismissProgress()
                adapter.setNewInstance(it)
                tvSampleBoxCount.text = "样本盒数量：${it.size}"
                /*model.requestList(id, {
                    dismissProgress()
                    adapter.setNewInstance(it)
                    tvSampleBoxCount.text = "样本盒数量：${it.size}"
                })*/
            }
        }
    }

    override fun onItemChildClick(adapter: BaseQuickAdapter<*, *>, view: View, position: Int) {
        val item = adapter.getItem(position) as PrepareData
        if(view.id == R.id.cbAll){
            if(checkPosition == position)return
            /*if(!TextUtils.isEmpty(item.rfidCode)){
                (adapter.data[position] as PrepareData).isChecked = false
                adapter.notifyItemChanged(position)
                showToast("已经绑定过样本盒RFID")
                return
            }*/
            if (checkPosition != -1) {
                (adapter.data[checkPosition] as PrepareData).isChecked = false
                adapter.notifyItemChanged(checkPosition)
            }
            (adapter.data[position] as PrepareData).isChecked = true
            adapter.notifyItemChanged(position)
            checkPosition = position
        }else{
            if(TextUtils.isEmpty(item.rfidCode)){
                showToast("还未绑定样本盒")
                return
            }
            val intent = Intent(this, ConsumableSampleListActivity::class.java)
            intent.putExtra("id", item.id)
            startActivity(intent)
        }
    }
}